package com.jiangchuang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jiangchuang.domain.Result;
import com.jiangchuang.domain.entity.Dish;
import com.jiangchuang.domain.entity.RiceDetail;
import com.jiangchuang.domain.response.GetReviewRes;
import com.jiangchuang.mapper.DishMapper;
import com.jiangchuang.service.IDishService;
import com.jiangchuang.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户管理")
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "用户登录（管理员）")
    @PostMapping("/userLogin")
    public Result<String> userLogin(String username, String password){
        return Result.ok(userService.userLogin(username, password));
    }

    @ApiOperation(value = "增加审核次数 type:0-图片，1-文字")
    @PostMapping("/api/addReviewNum")
    public Result<String> addReviewNum(Integer num, @RequestParam(defaultValue = "0") Integer type){
        userService.addReviewNum(num,type);
        return Result.ok();
    }

    @ApiOperation(value = "获取管理员审核总次数")
    @GetMapping("/api/getReviewNum")
    public Result<GetReviewRes> getReviewNum(Integer adminId){
        return Result.ok(userService.getReviewNum(adminId));
    }

    @ApiOperation(value = "重置当前审核次数")
    @GetMapping("/api/resetCurrentReview")
    public Result resetCurrentReview(Integer adminId){
        userService.resetCurrentReview(adminId);
        return Result.ok();
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/api/getUserInfo")
    public Result getUserInfo(Integer uid){
        return Result.ok(userService.getUserById(uid));
    }

    @ApiOperation(value = "更新用户金币数 ope:add/sub")
    @PostMapping("/api/editUserCoin")
    public Result<Integer> editUserCoin(Integer uid,String ope, Integer coinNum) {

        return Result.ok(userService.editUserCoin(uid,ope,coinNum));
    }

    @ApiOperation(value = "获取用户消费明细")
    @GetMapping("/getRiceDetail")
    public Result<PageInfo<RiceDetail>> getRiceDetail(Integer uid, @RequestParam(defaultValue = "1")int pageNo, @RequestParam(defaultValue = "10") int pageSize){
        return Result.ok(userService.getRiceDetail(uid, pageNo, pageSize));
    }

    @ApiOperation(value = "封禁账号")
    @GetMapping("/api/bannedUser")
    public Result<Integer> bannedUser(Integer uid){
        // 封禁
        userService.bannedUser(uid);
        // 注销
        clearUserInfo(uid);
        return Result.ok();
    }

    @ApiOperation(value = "注销用户")
    @GetMapping("/api/clearUserInfo")
    @Transactional
    public Result clearUserInfo(Integer uid){
        userService.clearUserInfo(uid);
        return Result.ok();
    }
}
